// 获取二级菜单id
var active_er_menu_id = localStorage.getItem('active_er_menu');
var active_san_menu_id = localStorage.getItem('active_er_menu'+active_er_menu_id);

// 获取二级菜单
var menu_arr = JSON.parse(localStorage.getItem('er_menu'));

// 遍历取出三级菜单
$.each(menu_arr, function(i, note) {
    if(active_er_menu_id == note.id){
        var control = '';
        // if(note.children && note.children.length > 0){
        //     $.each(note.children, function(i, note2) {
        //         if(active_san_menu_id == note2.id){
        //             control += '<option menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'" selected>'+note2.name+'</option>';
        //         }else{
        //             control += '<option menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'">'+note2.name+'</option>';
        //         }
        //     });
        // }else{
        //     control += '<option menu-url="'+note.modedir+'" menu-id="'+note.id+'" menu-pid="'+note.pid+'">'+note.name+'</option>';
        // }


        if(note.children && note.children.length > 0){
            $.each(note.children, function(i, note2) {
                if(active_san_menu_id == note2.id){
                    control += '<li class="layui-nav-item menu" menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'">';
                    control += '<div><a class="bottom-line">'+note2.name+'</a></div></li>';
                    // control += '<option menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'" selected>'+note2.name+'</option>';
                }else{
                    control += '<li class="layui-nav-item menu" menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'">';
                    control += '<div><a>'+note2.name+'</a></div></li>';
                    // control += '<option menu-url="'+note2.modedir+'" menu-id="'+note2.id+'" menu-pid="'+note2.pid+'">'+note2.name+'</option>';
                }
            });
        }else{
            control += '<li class="layui-nav-item menu" menu-url="'+note.modedir+'" menu-id="'+note.id+'" menu-pid="'+note.pid+'">';
            control += '<div><a class="bottom-line">'+note.name+'</a></div></li>';
            // control += '<option menu-url="'+note.modedir+'" menu-id="'+note.id+'" menu-pid="'+note.pid+'">'+note.name+'</option>';
        }

        setTimeout(function () {
            $('.layui-nav').prepend(control)
            console.log(control)
            // $("#san_menu").html(control).show();
        },300)

    }
});

// 三级菜单切换
setTimeout(function () {
    $('#san_menu').on('change',function () {
        var menu_id = $('#san_menu').find('option:selected').attr('menu-id');
        var menu_href = $('#san_menu').find('option:selected').attr('menu-url');
        var menu_pid = $('#san_menu').find('option:selected').attr('menu-pid');
        var menu_title = $(this).val();
        var obj = {id: menu_id, href: menu_href, title: menu_title, pid: menu_pid};

        // 跳转三级菜单设置缓存
        localStorage.setItem('active_san_menu',JSON.stringify(obj));

        // 设置当前页面的值
        var current = localStorage.getItem('content-laytp-tab-data-current');
        $("#san_menu").find("option").each(function () {
            $(this).attr('selected', false);
        });
        $("#san_menu").find("option[menu-id="+current+"]").attr("selected",true);
        // 生成或跳转页面
        parent.openNewPage(obj);
        return false;
    });
},300)


// 新页面跳转处理
window.addEventListener('storage',function (e) {
    if (e.key == 'active_er_menu_key') {
        if(e.newValue){
            $('.menu').each(function () {
                var key = $(this).attr('key');
                if(key == e.newValue){
                    $(this).find('a').addClass('bottom-line');
                }else{
                    $(this).find('a').removeClass('bottom-line');
                }
            });
        }else{
            $('.menu').each(function () {
                var key = $(this).attr('key');
                if(key == undefined){
                    $(this).find('a').addClass('bottom-line');
                }else{
                    $(this).find('a').removeClass('bottom-line');
                }
            });
        }
    }
},false);
